{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def search(text, pattern):\n",
    "    i, k = 0, len(pattern)\n",
    "    table = {c: k - i for i, c in enumerate(pattern)}\n",
    "\n",
    "    while True:\n",
    "        print(f'search @ {i}')\n",
    "        if text[i:i + k] == pattern:\n",
    "            print(f'FOUND @ {i}')\n",
    "        if i + k < len(text):\n",
    "            i += table.get(text[i + k], k + 1)\n",
    "        else:\n",
    "            break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "73"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = 'A parabolic (or paraboloid or paraboloidal) reflector (or dish or mirror)'\n",
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search @ 0\n",
      "search @ 10\n",
      "search @ 20\n",
      "search @ 30\n",
      "search @ 40\n",
      "search @ 44\n",
      "FOUND @ 44\n",
      "search @ 54\n",
      "search @ 56\n",
      "search @ 66\n"
     ]
    }
   ],
   "source": [
    "search(text, 'reflector')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search @ 0\n",
      "search @ 6\n",
      "search @ 10\n",
      "search @ 11\n",
      "search @ 17\n",
      "search @ 33\n",
      "search @ 40\n",
      "search @ 44\n",
      "search @ 60\n"
     ]
    }
   ],
   "source": [
    "search(text, 'not to be found')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
